package br.edu.ufcg.dsc.actions.user;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import br.edu.ufcg.dsc.domain.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BaseUserAction extends ActionSupport implements ModelDriven<User>,
		Preparable {

	protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory("sobi");
	protected static EntityManager entity = emf.createEntityManager();
	
	private static final long serialVersionUID = -4498813710855026576L;

	protected User user;

	protected String loginId;

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public User getModel() {
		return user;
	}

	public void prepare() throws Exception {
		if (loginId == null	|| (user = entity.find(User.class, getLoginId())) == null)
			user = new User();
	}
}
